package com.bootdo.blog.controller;

import com.bootdo.blog.domain.ContentDO;
import com.bootdo.blog.service.ContentService;
import com.bootdo.common.config.Constant;
import com.bootdo.common.controller.BaseController;
import com.bootdo.common.utils.FileUtil;
import com.bootdo.common.utils.OssUtil;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;
import com.bootdo.common.utils.ShiroUtils;
import com.bootdo.oa.domain.NotifyDO;
import com.bootdo.oa.service.NotifyService;
import com.bootdo.system.domain.UserDO;
import com.bootdo.system.domain.UserRoleDO;
import com.bootdo.system.service.UserRoleService;
import com.bootdo.system.service.UserService;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

/**
 * 文章内容
 * 
 * @author chglee
 * @email 1992lcg@163.com
 * @date 2017-09-09 10:03:34
 */
@Controller
@RequestMapping("/blog/bContent")
public class ContentController extends BaseController {
	@Autowired
    ContentService bContentService;
	
	@Autowired
	NotifyService notifyService;
	
	@Autowired
	UserService userService;
	@Autowired
	UserRoleService userRoleService;

	@GetMapping()
	String bContent() {
		return "blog/bContent/bContent";
	}

	@ResponseBody
	@GetMapping("/list")
	/*@RequiresPermissions("blog:bContent:bContent")*/
	public PageUtils list(@RequestParam Map<String, Object> params) {
		Query query = new Query(params);
		List<ContentDO> bContentList = bContentService.list(query);
		int byUserId = 0;
		try {
			byUserId = userRoleService.getByUserId(ShiroUtils.getUserId());
		} catch (NullPointerException e) {
			return null;
		}
		int total = bContentService.count(query);
		PageUtils pageUtils = new PageUtils(bContentList, total,byUserId);
		return pageUtils;
	}

	@GetMapping("/add")
	@RequiresPermissions("blog:bContent:add")
	String add(Model model) {
		UserDO userDO = userService.get(getUserId());
		model.addAttribute("author", userDO.getCompanyName());
		return "blog/bContent/add";
	}
	
	@GetMapping("/mobileadd")
	@ResponseBody
	R mobileadd(Model model) {
		UserDO user = (UserDO) SecurityUtils.getSubject().getPrincipal();
		try {
			/*UserDO userDO = userService.get(getUserId());*/
			//model.addAttribute("author", userDO.getCompanyName());
			return R.ok(user.getCompanyName());
		} catch (Exception e) {
			return R.error("no");
		}
		
	}

	@GetMapping("/edit/{cid}")
	@RequiresPermissions("blog:bContent:edit")
	String edit(@PathVariable("cid") Long cid, Model model) {
		ContentDO bContentDO = bContentService.get(cid);
		model.addAttribute("bContent", bContentDO);
		return "blog/bContent/edit";
	}
	

	
	
	
	

	@GetMapping("/reply/{id}")
	String reply(@PathVariable("id") Long id, Model model) {
		model.addAttribute("id",id);
		return "blog/bContent/reply";
	}
	
	/**
	 * 
	 * @Title: save   
	 * @Description: TODO(投诉的方法！！！)   
	 * @return: R
	 * @author:liuziyu        
	 * @date:2018年4月26日
	 */
	@ResponseBody
	@PostMapping("/save")
	public R save(ContentDO bContent) {
		bContent.setGtmCreate(new Date());
		try {
			bContent.setCreated(getUserId());
		} catch (Exception e) {
			return R.error("no");
		}
		
		bContent.setStatus(0);
	    int save = bContentService.save(bContent);
	    if(save==1){
	    	NotifyDO notify = new NotifyDO();
			notify.setCreateBy((long)getUserId());
			notify.setType("3");// 通知类型 1:邀请 2:活动 3:任务
			notify.setTitle("新投诉");
			notify.setContent("您有新的投诉请尽快处理！");
			notify.setRemarks("投诉信息请在投诉列表中查看");
			notify.setStatus("1");
			notify.setCreateDate(new Date());
			Long[] userIds = {Constant.SQL_NUMBER} ;
			notify.setUserIds(userIds);
			notifyService.save(notify);
			return R.ok("发送成功");
	    }else {
			return R.error("发送失败");
		}
	
	}
	
	/**
	 * @Title: reply   
	 * @Description: TODO(回复投诉的方法)   
	 * @return: R
	 * @author:liuziyu        
	 * @date:2018年4月26日
	 */
	@ResponseBody
	@PostMapping("/reply")
	public R reply(long id,String title,String content) {
		ContentDO contentDO = bContentService.get(id);
		Long created = contentDO.getCreated();
		Long[] userid = {created};
		ContentDO bContent = new ContentDO();
		bContent.setCid(id);
		bContent.setStatus(1);
	    bContentService.update(bContent);
		NotifyDO notify = new NotifyDO();
		notify.setType("3");
		notify.setStatus("1");
		notify.setCreateDate(new Date());
		notify.setTitle(title);
		notify.setCreateBy(Constant.SQL_NUMBER);
		notify.setUserIds(userid);
		notify.setContent(content);
		notify.setRemarks("感谢你的支持。我们会做得更好");
		notifyService.save(notify);
		return R.ok();
	}
	

	/**
	 * 修改
	 */
	@RequiresPermissions("blog:bContent:edit")
	@ResponseBody
	@RequestMapping("/update")
	public R update(ContentDO bContent) {
		bContent.setGtmCreate(new Date());
		bContentService.update(bContent);
		return R.ok();
	}

	/**
	 * 删除
	 */
	@RequiresPermissions("blog:bContent:remove")
	@PostMapping("/remove")
	@ResponseBody
	public R remove(Long id) {
		if (bContentService.remove(id) > 0) {
			return R.ok();
		}
		return R.error();
	}
     
	
	@ResponseBody
	@PostMapping("/upload")
	R upload(@RequestParam("file") MultipartFile file, HttpServletRequest request) {
		String url =null;
		try {
			url = OssUtil.fileUpload(file);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return R.ok().put("fileName",url);
	}
	
	
	
	/**
	 * 删除
	 */
	@RequiresPermissions("blog:bContent:batchRemove")
	@PostMapping("/batchRemove")
	@ResponseBody
	public R remove(@RequestParam("ids[]") Long[] cids) {
		bContentService.batchRemove(cids);
		return R.ok();
	}
	@ResponseBody
	@RequestMapping("/contentUser")
	public UserDO ContentUserId(){
		UserDO user = ShiroUtils.getUser();
		return user;
	}
}
